package com.ebook.ui.fragment.home.category;

import com.ebook.ui.activity.BookFindActivity;
import com.ebook.ui.activity.HomeActivity;
import com.ebook.ui.logic.BookDistributer;
import com.ebook.ui.sync.WorkSyncer;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class BookCategoryLink extends JLabel {
    /**
     * params:
     * String name 书籍分类的名字
     * int position 书籍分类所居于的位置,SwingConstants.LEFT,.CENTER,.RIGHT
     * **/
    public BookCategoryLink(String name,int position){
        super(name,position);
//        this.setBorder(new LineBorder(Color.BLACK));
        this.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e){

                SwingUtilities.invokeLater(()->{
                    try {
                        WorkSyncer.webView.pushActivity(
                                new BookFindActivity(BookDistributer.distributer(name))
                        );
                    } catch (NoSuchFieldException | IllegalAccessException ex) {
                        ex.printStackTrace();
                    }
                    System.out.println(name);
                });
            }
            @Override
            public void mouseEntered(MouseEvent e){
                BookCategoryLink.this.setCursor(new Cursor(Cursor.HAND_CURSOR));
            }
        });
        setBackground(Color.white);

    }
    /**
     * params:
     * String name 书籍分类的名字
     * **/
    public BookCategoryLink(String name){
        super(name);
        this.setBorder(new LineBorder(Color.BLACK));
        this.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e){

                SwingUtilities.invokeLater(()->{
                    if(WorkSyncer.webView.peekActivity() instanceof HomeActivity){
                        try {
                            WorkSyncer.webView.pushActivity(
                                    new BookFindActivity(BookDistributer.distributer(name))
                            );
                        } catch (NoSuchFieldException | IllegalAccessException ex) {
                            ex.printStackTrace();
                        }
                        System.out.println(name);
                    }else if(WorkSyncer.webView.peekActivity() instanceof BookFindActivity){
                        ((BookFindActivity) WorkSyncer.webView.peekActivity()).updateUiByCategory(name);
                    }
                });

            }
        });
        setBackground(Color.white);
    }
}
